//
//  BRPointRuleTbCell.m
//  Kexu
//
//  Created by IOS on 2023/7/28.
//

#import "BRPointRuleTbCell.h"

@implementation BRPointRuleTbCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}

- (void)toAction {
    
    if (self.actionBlock) self.actionBlock(self.type);
}
/// 类型：1-新用户注册 2-每日访问APP 3-租借 4-绑卡 5-报失
- (void)setType:(NSInteger)type {
    _type = type;
    
    self.compL.hidden = type !=1;
    BOOL hasBtn = type == 3 || type == 4;
    self.acBtn.hidden = !hasBtn;
    
    if (hasBtn) {
        
        NSString *title = type == 3 ? Localized(@"points_gotoRent") : Localized(@"points_addNow");
        [self.acBtn setTitle:title forState:UIControlStateNormal];
    }
}

- (void)setDto:(BRPointRuleModel *)dto {
    _dto = dto;
   
    NSString *lanKey = [[[BRLanguageManager share] requestStr] lowercaseString];
    NSString *title = [dto.title valueForKey:lanKey];
    NSString *text = [dto.descriptionDic valueForKey:lanKey];
    if (title.length == 0) title = @"--";
    if (text.length == 0) text = @"--";
    NSString *titleApp = [NSString stringWithFormat:@"%@(%@)",title,text];
    
    self.titleL.text = titleApp;
    self.textL.text = [NSString stringWithFormat:@"%@: %ld",Localized(@"menu_points"),dto.point];
    
    self.type = dto.type;
}

#pragma mark - UI --
- (void)setUI {
    
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    UIView *contView = [UIView new];
    contView.backgroundColor = UIColor.whiteColor;
    contView.layer.cornerRadius = 10;
    contView.layer.masksToBounds = YES;
    [self.contentView addSubview:contView];
    [contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.left.top.offset(14);
        make.right.offset(-14);
    }];
    
    self.titleL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontS:17] textAlignment:NSTextAlignmentLeft];
    self.textL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:14] textAlignment:NSTextAlignmentLeft];
    self.compL = [BRToolsView br_label:Localized(@"points_completed") textColor:UIColorMakeWithHex(@"#4CE48A") font:[UIFont fontM:14] textAlignment:NSTextAlignmentRight];
    
    self.titleL.numberOfLines = 0;
    
    [contView addSubview:self.titleL];
    [contView addSubview:self.textL];
    [contView addSubview:self.compL];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.left.offset(14);
        make.right.offset(-120);
    }];
    [self.textL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleL.mas_bottom).offset(10);
        make.left.offset(14);
        make.bottom.offset(-20);
    }];
    [self.compL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-20);
        make.top.equalTo(self.titleL.mas_top).offset(0);
    }];
    
    self.compL.hidden = YES;
    
    self.acBtn = [[QMUIButton alloc] init];
    [self.acBtn setTitle:Localized(@"--") forState:UIControlStateNormal];
    [self.acBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.acBtn.titleLabel.font = [UIFont fontM:14];
    self.acBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 14, 0, 14);
//    self.acBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.acBtn.titleLabel.numberOfLines = 2;
    [self.acBtn setBackgroundColor:kColorMain];
    self.acBtn.layer.cornerRadius = 18;
    self.acBtn.layer.masksToBounds = YES;
    [contView addSubview:self.acBtn];
    [self.acBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.mas_greaterThanOrEqualTo(36);
        make.right.offset(-14);
        make.width.mas_lessThanOrEqualTo(120);
    }];
    [self.acBtn addTarget:self action:@selector(toAction) forControlEvents:UIControlEventTouchUpInside];
}

@end


@implementation BRPointListTbCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}

- (void)setDto:(BRPointHistoryModel *)dto {
    _dto = dto;
    
    
    NSString *time = [BRTools getTimeFromTimestamp:[dto.createTime integerValue] formatter:kTimeFormatter];
    self.textL.text = time;
    NSString *symbolStr = self.type == 0 ? @"+" : @"";
    self.pointL.text = [NSString stringWithFormat:@"%@%ld",symbolStr,dto.point];
    self.allL.text = [NSString stringWithFormat:@"%ld",dto.currentPoint];
    
   
    if (dto.type == 7 || dto.type == 8) {
        
        NSInteger couponType = dto.couponInfo.deductionType;
        NSString *title1 = [BRStringTools br_couponsTypeStr:couponType];
        NSString *text1 = [BRStringTools br_couponsTypeContentV2:dto.couponInfo.deduction];
        
        self.titleL.text = [NSString stringWithFormat:@"%@ %@",title1,text1];
        
    }else {
        // 后台返回
        NSString *lanKey = [[[BRLanguageManager share] requestStr] lowercaseString];
        NSString *title = [dto.title valueForKey:lanKey];
        if (title.length == 0) title = @"--";
        NSString *titleApp = [NSString stringWithFormat:@"%@",title];
        self.titleL.text = titleApp;
    }
    
}

- (NSString *)pointUseType:(NSInteger)type {
    
    NSString *str = [NSString stringWithFormat:@"Rewards_pointHistoryType%ld",type];
    return Localized(str);
}

#pragma mark - UI --
- (void)setUI {
    
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    UIView *contView = [UIView new];
    contView.backgroundColor = UIColor.whiteColor;
    contView.layer.cornerRadius = 10;
    contView.layer.masksToBounds = YES;
    [self.contentView addSubview:contView];
    [contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.left.top.offset(14);
        make.right.offset(-14);
    }];
    
    self.titleL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontS:17] textAlignment:NSTextAlignmentLeft];
    self.textL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:14] textAlignment:NSTextAlignmentLeft];
    self.pointL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontB:17] textAlignment:NSTextAlignmentRight];
    self.allL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:14] textAlignment:NSTextAlignmentRight];
    
    self.titleL.numberOfLines = 0;
    
    [contView addSubview:self.titleL];
    [contView addSubview:self.textL];
    [contView addSubview:self.pointL];
    [contView addSubview:self.allL];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.left.offset(14);
        make.right.offset(-60);
    }];
    [self.textL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleL.mas_bottom).offset(10);
        make.left.offset(14);
        make.bottom.offset(-20);
    }];
    [self.pointL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-14);
        make.top.equalTo(self.titleL.mas_top).offset(0);
    }];
    [self.allL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-14);
        make.top.equalTo(self.textL.mas_top).offset(0);
    }];
    
    
}
@end
